package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.zzw.TEventManage;
import com.ruoyi.common.core.domain.zzw.TIndicatorsInfo;
import com.ruoyi.common.core.domain.zzw.VO.JsonToPojo;
import com.ruoyi.common.core.domain.zzw.VO.NewRenderingData;
import com.ruoyi.common.core.domain.zzw.VO.TIndicatorsList;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ITEventManageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TIndicatorsInfoMapper;
import com.ruoyi.system.service.ITIndicatorsInfoService;

/**
 * 指标Service业务层处理
 *
 * @author xbx
 * @date 2023-10-20
 */
@Service
public class TIndicatorsInfoServiceImpl extends ServiceImpl<TIndicatorsInfoMapper, TIndicatorsInfo> implements ITIndicatorsInfoService {
    @Autowired
    private TIndicatorsInfoMapper tIndicatorsInfoMapper;
    @Autowired
    private ITEventManageService eventManageService;
    /**
     * 查询指标
     *
     * @param cIndicatorsId 指标主键
     * @return 指标
     */
    @Override
    public TIndicatorsInfo selectTIndicatorsInfoByCIndicatorsId(Long cIndicatorsId) {
        return tIndicatorsInfoMapper.selectTIndicatorsInfoByCIndicatorsId(cIndicatorsId);
    }

    /**
     * 查询指标列表
     *
     * @param tIndicatorsInfo 指标
     * @return 指标
     */
    @Override
    public List<TIndicatorsInfo> selectTIndicatorsInfoList(TIndicatorsInfo tIndicatorsInfo) {
        return tIndicatorsInfoMapper.selectTIndicatorsInfoList(tIndicatorsInfo);
    }

    /**
     * 新增指标
     *
     * @param tIndicatorsInfo 指标
     * @return 结果
     */
    @Override
    public int insertTIndicatorsInfo(TIndicatorsInfo tIndicatorsInfo) {
        return tIndicatorsInfoMapper.insertTIndicatorsInfo(tIndicatorsInfo);
    }

    /**
     * 修改指标
     *
     * @param tIndicatorsInfo 指标
     * @return 结果
     */
    @Override
    public int updateTIndicatorsInfo(TIndicatorsInfo tIndicatorsInfo) {
        return tIndicatorsInfoMapper.updateTIndicatorsInfo(tIndicatorsInfo);
    }


    /**
     * 批量删除指标
     *
     * @param cIndicatorsIds 需要删除的指标主键
     * @return 结果
     */
    @Override
    public int deleteTIndicatorsInfoByCIndicatorsIds(List<Long> cIndicatorsIds) {
        return tIndicatorsInfoMapper.deleteTIndicatorsInfoByCIndicatorsIds(cIndicatorsIds);
    }

    /**
     * 删除指标信息
     *
     * @param cIndicatorsId 指标主键
     * @return 结果
     */
    @Override
    public int deleteTIndicatorsInfoByCIndicatorsId(Long cIndicatorsId) {
        return tIndicatorsInfoMapper.deleteTIndicatorsInfoByCIndicatorsId(cIndicatorsId);
    }

    @Override
    public TIndicatorsInfo getByIndicatorsId(Long indicatorsId) {
        QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TIndicatorsInfo::getCIndicatorsId, indicatorsId)
                .eq(TIndicatorsInfo::getCIsDelete, 1)
        ;
        return this.getOne(queryWrapper);
    }

    @Override
    public List<TIndicatorsInfo> listByCModuleId(String cModuleId) {

        QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TIndicatorsInfo::getCModuleId, cModuleId)
                .eq(TIndicatorsInfo::getCDistrictId, 1)
                .eq(TIndicatorsInfo::getCIsDelete, 1)
                .orderByAsc(TIndicatorsInfo::getCIndicatorsSite)
                .orderByAsc(TIndicatorsInfo::getCOrderNo)
        ;
        return this.list(queryWrapper);

    }

    @Override
    public List<TIndicatorsInfo> listByCDistrictId(String cDistrictId) {
        QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TIndicatorsInfo::getCDistrictId, cDistrictId)
                .eq(TIndicatorsInfo::getCIsDelete, 1)
                .orderByAsc(TIndicatorsInfo::getCOrderNo)
                .orderByAsc(TIndicatorsInfo::getCIndicatorsSite)
        ;
        return this.list(queryWrapper);
    }


    @Override
    public List<TIndicatorsList> listByModuleAndDistrict(String moduleId, String districtId) {
        QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TIndicatorsInfo::getCModuleId, moduleId)
                .eq(TIndicatorsInfo::getCDistrictId, districtId)
                .eq(TIndicatorsInfo::getCIsDelete, 1)
                .orderByAsc(TIndicatorsInfo::getCOrderNo)
                .orderByAsc(TIndicatorsInfo::getCIndicatorsSite)
        ;
        List<TIndicatorsInfo> list = this.list(queryWrapper);
        List<TIndicatorsList> vo=new ArrayList<>();
        for (TIndicatorsInfo tIndicatorsInfo : list) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                TIndicatorsList tIndicators=new TIndicatorsList();

                if (StringUtils.isNotEmpty(tIndicatorsInfo.getCIndicatorsType()) && !tIndicatorsInfo.getCIndicatorsType().equals("8") ){
                    BeanUtils.copyProperties(tIndicatorsInfo,tIndicators);
                    List<JsonToPojo> indicators = objectMapper.readValue(tIndicatorsInfo.getCIndicatorsData(), new TypeReference<List<JsonToPojo>>() {});
                    if (tIndicators.getCIsRendering()==1){
                        List<NewRenderingData> newRenderingData = objectMapper.readValue(tIndicatorsInfo.getCRenderingData(), new TypeReference<List<NewRenderingData>>() {
                        });
                        tIndicators.setRendering(newRenderingData);
                        tIndicators.setCRenderingData(null);
                    }
//                    List<JsonToPojo> drillDown = objectMapper.readValue(tIndicatorsInfo.getCDrillDownData(), new TypeReference<List<JsonToPojo>>() {});
                    tIndicators.setIndicators(indicators);

//                    tIndicators.setDrillDown(drillDown);
                    tIndicators.setCIndicatorsData(null);
                    vo.add(tIndicators);
                }else {
                    BeanUtils.copyProperties(tIndicatorsInfo,tIndicators);
                    vo.add(tIndicators);
                }
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        List<TIndicatorsList> collect = vo.stream()
                .sorted(Comparator.comparing(TIndicatorsList::getCOrderNo))
                .collect(Collectors.toList());
        return collect;
    }


    @Override
    public List<TEventManage> getRelatedEvent(String cIndicatorsId) {
        List<TEventManage> eventList = new ArrayList<>();
        String relatedKeyEventId = tIndicatorsInfoMapper.selectRelatedKeyEventId(cIndicatorsId);
        if (StringUtils.isBlank(relatedKeyEventId)) {
            return eventList;
        }
        TEventManage event = eventManageService.getById(relatedKeyEventId);
        eventList.add(event);
        if (StringUtils.isBlank(event.getcSubEventList())) {
            String[] split = event.getcSubEventList().split(",");
            List<String> list = Arrays.asList(split);
            List<TEventManage> subList = eventManageService.listByIds(list);
            eventList.addAll(subList);
        }
        return eventList;
    }

}
